import {
  getWaitCheckWorkReassign as getWaitCheckWorkReassignApi,
  checkWorkReassign as checkWorkReassignApi,
} from '@/api/order'
import { ref } from 'vue'
import { CheckWorkReassignAllow, CheckWorkReassignReject, WaitCheckReassign } from '../types'

// 待审核改派服务商的工单
const waitCheckWorkReassign = ref<WaitCheckReassign | null>(null)

// 获取待审核改派服务商的工单加载
const getWaitCheckWorkReassignLoading = ref(false)

/**
 * 获取待审核改派服务商的工单方法
 * @param {string} id - 工单id
 */
const getWaitCheckReassign = async (id: string) => {
  getWaitCheckWorkReassignLoading.value = true
  const res = await getWaitCheckWorkReassignApi(id)
  getWaitCheckWorkReassignLoading.value = false
  waitCheckWorkReassign.value = res.data.data.row
}

// 审核改派服务商的工单加载
const checkWorkReassignLoading = ref(false)

/**
 * 通过审核改派服务商的工单
 * @param {Object} param - 请求参数
 * @param {String} param.changeToNetworkId - 新服务商ID
 * @param {String} param.id - 改派工单ID
 * @param {String} param.networkEmpId - 新服务商工程师ID
 * @param {String} param.remark - 备注
 * @param {string} workId - 工单id
 */
const allow = async (param: CheckWorkReassignAllow, workId: string) => {
  checkWorkReassignLoading.value = true
  const res = await checkWorkReassignApi(Object.assign(param, { checkTypep: 1 }), workId)
  checkWorkReassignLoading.value = false
  return res.data as { message: string; success: boolean; data: object; code: number }
}

/**
 * 驳回审核改派服务商的工单
 * @param {Object} param - 请求参数
 * @param {String} param.id - 改派工单ID
 * @param {String} param.remark - 备注
 * @param {string} workId - 工单id
 */
const reject = async (param: CheckWorkReassignReject, workId: string) => {
  checkWorkReassignLoading.value = true
  const res = await checkWorkReassignApi(
    Object.assign(param, { checkTypep: 2, changeToNetworkId: '', networkEmpId: '' }),
    workId,
  )
  checkWorkReassignLoading.value = false
  return res.data as { message: string; success: boolean; data: object; code: number }
}

export default {
  waitCheckWorkReassign,
  checkWorkReassignLoading,
  getWaitCheckWorkReassignLoading,
  allow,
  reject,
  getWaitCheckReassign,
}
